package com.wt.service.impl;

import com.wt.dao.WqunAccountDao;
import com.wt.entity.WqunAccount;
import com.wt.entity.utilEntity.login.LoginAccount;
import com.wt.service.WqunAccountService;
import com.wt.util.R.R;
import com.wt.util.ResultMess.ResultMess;

import java.util.List;

public class WqunAccountServiceImpl implements WqunAccountService {

    WqunAccountDao wqunAccountDao = new WqunAccountDao();
    @Override
    public R selectAccountByPhone(String phone) {

        List<WqunAccount> wqunAccounts = wqunAccountDao.addAccountByPhone(phone);
        if(wqunAccounts != null && wqunAccounts.size() != 0){
            return R.ok().message(ResultMess.FIND_PHONE_TO_USER_SUCCESS.getDesc()).data("selectAccountByPhone",wqunAccounts.get(0));
        }else{
            return R.error().message(ResultMess.FIND_PHONE_IS_NULL.getDesc());
        }


    }

    @Override
    public R selectByNickName(String nickName) {

        List<WqunAccount> wqunAccounts = wqunAccountDao.selectByNickName(nickName);
        if(wqunAccounts != null && wqunAccounts.size() != 0){
            return R.ok().message(ResultMess.FIND_SUCCESS.getDesc()).data("WqunAccountServiceImpl",wqunAccounts.get(0));
        }else{
            return R.error().message(ResultMess.FIND_PHONE_IS_NULL.getDesc());
        }
    }

    @Override
    public boolean save(WqunAccount wqunAccount) {
        return wqunAccountDao.save(wqunAccount);
    }

    @Override
    public WqunAccount getOne(LoginAccount loginAccount) {
        List<WqunAccount> wqunAccounts = wqunAccountDao.selectWqunAccountPhone(loginAccount.getAccountPhone());
        if(wqunAccounts != null && wqunAccounts.size()>0){
            return wqunAccounts.get(0);
        }else{
            return null;
        }
    }

    @Override
    public WqunAccount getOneById(int id) {
        List<WqunAccount> wqunAccounts = wqunAccountDao.getOneById(id);
        if(wqunAccounts != null && wqunAccounts.size()>0){
            return wqunAccounts.get(0);
        }else{
            return null;
        }
    }

    @Override
    public boolean updatePass(WqunAccount wqunAccount) {
        return wqunAccountDao.updatePass(wqunAccount);
    }

    @Override
    public boolean update(WqunAccount wqunAccount) {
        return wqunAccountDao.update(wqunAccount);
    }

    @Override
    public boolean deleteById(int id) {
        return wqunAccountDao.deleteById(id);
    }

    @Override
    public R findAccountPage(int pageSize, int currentPage, String selectValue, String inputValue) {
        currentPage--;

        List<WqunAccount> findAccountPage = wqunAccountDao.findAccountPage(pageSize, currentPage, selectValue, inputValue);
//        System.out.println("findAccountPage = " + findAccountPage);
        if(findAccountPage != null && findAccountPage.size() != 0){
            int accountPage = findAccountCount();
            return R.ok().message("查询成功").data("findAccountPage",findAccountPage).data("total",accountPage);
        }else{
            return R.error().message("请检查网络");
        }
    }

    @Override
    public List<WqunAccount> listAll() {
        return wqunAccountDao.listAll();
    }

    public int findAccountCount() {
        int resCount = wqunAccountDao.findAccountCount();
        return resCount;
    }
}
